package com.xingkeduo.business.operation.controller;

import com.alibaba.fastjson.JSON;
import com.xingkeduo.business.operation.domain.PictureDomain;
import com.xingkeduo.pojo.dto.Picture.PictureDTO;
import com.xingkeduo.pojo.json.PictureJson;
import com.xingkeduo.pojo.query.PictureQuery;
import com.xingkeduo.pojo.table.PictureTable;
import com.xingkeduo.tool.ResponseUtils;
import com.xingkeduo.util.pojo.exception.SystemException;
import com.xingkeduo.util.tool.ReturnUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.xingkeduo.tool.OperaConstants.CMS_URL_PREFIX;

/**
 *
 * @author chenxiangda <chenxiangda@xingkeduo.com>
 * @version id:v0.1 ${date}${time}
 * @package BeautyOS
 * @copyright Copyright (c) XKD 2019
 */
@Slf4j
@RestController
@RequestMapping(CMS_URL_PREFIX +"/picture")
public class PictureController extends InitController {

    @Autowired
    private PictureDomain pictureDomain;

    //查看
    @RequestMapping(value = "/{urlParam:.+}", method = RequestMethod.GET)
    public String doGet(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            long id = PictureJson.createQueryForPicture(urlParam);
            PictureTable pictureTable = pictureDomain.getPictureById(id);
            return returnUtil.getSuccess(PictureJson.toJson(pictureTable));
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //添加
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseUtils doPost(@RequestBody @Valid PictureDTO pictureDTO  , ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
            pictureDTO.setCreatedBy(this.getOperator(request));
            log.info("添加图片接口请求参数:{}", JSON.toJSONString(pictureDTO));
            Long picture = pictureDomain.createPicture(pictureDTO);
            log.info("添加图片接口返回值:{}", JSON.toJSONString(picture));
            return ResponseUtils.getSuccess(picture);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return ResponseUtils.error(throwable.getMessage(), SystemException.Type.DEFAULT.toString());
        }
    }


    //修改
    @RequestMapping(value = "/{urlParam}", method = RequestMethod.PUT)
    public String doPut(@PathVariable String urlParam, @RequestBody @Valid PictureTable pictureVo  , ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
            //this.errMsg(bindingResult);
            PictureTable pictureTable = PictureJson.createTableForModifyPicture(urlParam,pictureVo);
            pictureVo.setUpdatedBy(this.getOperator(request));
            log.info("修改图片接口请求参数:{}", JSON.toJSONString(pictureVo));
            int picture = pictureDomain.modifyPicture(pictureTable);
            log.info("修改图片接口返回值:{}", JSON.toJSONString(picture));
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //删除
    @RequestMapping(value = "/{urlParam}", method = RequestMethod.DELETE)
    public String doDelete(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            long id  = PictureJson.createQueryForPicture(urlParam);
            pictureDomain.delPicture(id);
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    @RequestMapping(value = "/lists/{urlParam:.+}", method = RequestMethod.GET)
    public String doGetLists(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            PictureQuery pictureQuery = new PictureQuery();

            pictureQuery=  JSON.parseObject(urlParam,PictureQuery.class);

            List<PictureTable> pictureList= pictureDomain.getPictureList(pictureQuery);

            return returnUtil.getSuccess(PictureJson.toJson(pictureList));
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }
}
